//配置路由信息

//引入路由组件
// import Home from '@/pages/Home'
// import Search from '@/pages/Search'
//路由懒加载也可以这样写，下面引入就可以了
const Search = () => import('@/pages/Search')

import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import MyOrder from '@/pages/Center/myOrder'
import GroupOrder from '@/pages/Center/groupOrder'


/* 【路由懒加载】例子

component: () => import('@/pages/Search')
1. import(modulePath): 动态import引入模块, 被引入的模块会被单独打包
2. 组件配置的是一个函数, 函数中通过import动态加载模块并返回, 
    初始时函数不会执行, 第一次访问对应的路由才会执行, 也就是说只有一次请求对应的路由路径才会请求加载单独打包的js
作用: 用于提高首屏的加载速度
*/

export default [

    {
        path: "/",
        redirect: "/home",
        //路由信息key只能写mate
        meta: { show: true }
    },
    {   //路由懒加载
        path: "/home",
        //上面定义了Search
        component:Search,
        meta: { show: true }
    },
    {   //路由懒加载
        name: "search",
        path: "/search/:keyword?",
        component:() => import('@/pages/Search'),
        meta: { show: true },
         // 将params参数和query参数映射成属性传入路由组件
        props: route => ({keyword3: route.params.keyword, keyword4: route.query.keyword2})
    },
    {
        path: "/login",
        component: Login
    },
    {
        path: "/register",
        component: Register
    },
    {
        path: "/detail/:skuId",
        component: Detail,
        meta: { show: true }
    },
    {
        path: "/addcartsuccess",
        name: 'addcartsuccess',
        component: AddCartSuccess,
        meta: { show: true }
    },
    {
        path: "/shopcart",
        component: ShopCart,
        meta: { show: true }
    },
    {
        path: "/trade",
        component: Trade,
        meta: { show: true },
        /* 只能从购物车界面, 才能跳转到交易界面 */
        beforeEnter(to, from, next) {
            if (from.path === '/shopcart') {
                next()
            } else {
                next('/shopcart')
            }
        }
    },
    {
        path: "/pay",
        component: Pay,
        meta: { show: true },
        /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter (to, from, next) {
        if (from.path==='/trade') {
          next()
        } else {
          next('/trade')
        }
      }
    },
    {
        path: "/paysuccess",
        component: PaySuccess,
        meta: { show: true },
         /* 只有从支付界面, 才能跳转到支付成功的界面 */
    beforeEnter (to, from, next) {
        if (from.path==='/pay') {
          next()
        } else {
          next('/pay')
        }
      }
    },
    {
        path: "/center",
        component: Center,
        meta: { show: true },
        children: [
            {
                path: "myOrder",
                component: MyOrder,
            },
            {
                path: "groupOrder",
                component: GroupOrder,
            },
            {
                path: "/center",
                redirect: '/center/myOrder'

            }

        ]
    }
]